/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import path from 'path';

import { hvigorCore } from '@ohos/hvigor';
import { isCI } from '@ohos/hvigor-common';

import { BuildArtifactConst, BuildDirConst } from '../../const/build-directory-const.js';
import { CommonConst, UnitTestConst } from '../../const/common-const.js';
import { CoreModuleModelImpl } from '../../model/module/core-module-model-impl.js';
import { ModuleModel } from '../../model/module/module-model.js';
import { ProjectBuildProfile } from '../../options/build/project-build-profile.js';
import { InjectUtil } from '../../utils/inject-util.js';
import { LegacyPathInfo } from '../legacy-path-info.js';
import { PathInfo } from '../path-info.js';
import { getBuildModeName } from '../../utils/task-util.js';
import { CodeType } from '../../enum/code-type-enum.js';
import { TargetTaskService } from '../../tasks/service/target-task-service.js';
import fse from 'fs-extra';

export class ModulePathInfoIml implements LegacyPathInfo, PathInfo {
  private readonly _curModule: ModuleModel;
  private readonly _targetName: string;
  private readonly _modulePath: string;
  private readonly _productName: string;
  private readonly _buildRoot: string;

  constructor(curModule: ModuleModel, targetName: string, productName: string, buildRoot = BuildDirConst.BUILD_ROOT) {
    this._curModule = curModule;
    this._targetName = targetName;
    this._modulePath = curModule.getProjectDir();
    this._productName = productName;
    this._buildRoot = buildRoot;
  }

  getPatchDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.PATCH, this._targetName);
  }

  getObfuscationExportRulePath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'obfuscation', this._targetName, CommonConst.OBFUSCATION_TXT);
  }

  getIntermediatesJavaDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'java', this._targetName);
  }

  getIntermediatesClassDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'class', this._targetName);
  }

  getIntermediatesDexDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'dex', this._targetName);
  }

  getIntermediatesShellClassDir(): string {
    return path.resolve(this.getModuleShellDir(), this.getBuildRoot(), 'class');
  }

  getIntermediatesShellDexDir(): string {
    return path.resolve(this.getModuleShellDir(), this.getBuildRoot(), 'dex');
  }

  getIntermediatesShellJavaDir(): string {
    return path.resolve(this.getShellSrcMain(), 'java');
  }

  getIntermediatesApkDir(): string {
    return path.resolve(this.getModuleShellDir(), this.getBuildRoot(), 'apk');
  }

  getBuildRoot(): string {
    return this._buildRoot;
  }

  getModuleBuildPath(supportBuildCacheDir = true): string {
    return path.resolve(
      InjectUtil.getBuildCacheParentDir(
        this._modulePath,

        // Safety: hvigor.getProject()必然已经被初始化
        path.join(hvigorCore.getProject()!.getName(), this._modulePath.substring(hvigorCore.getProject()!.getNodeDir().length)),
        supportBuildCacheDir,
      ),
      this._buildRoot,
    );
  }

  getModuleSrcPath(): string {
    return path.resolve(this._modulePath, BuildDirConst.SRC);
  }

  getModuleSrcMainPath(): string {
    return path.resolve(this._modulePath, BuildDirConst.SRC, BuildDirConst.MAIN);
  }

  getModuleSrcMockPath() {
    return path.resolve(this._modulePath, BuildDirConst.SRC, BuildDirConst.MOCK);
  }

  getModulePath() {
    return this._modulePath;
  }

  getModuleSrcMainResourceRawfileArkdataPath(): string {
    return path.resolve(this.getModuleSrcMainPath(), BuildDirConst.RESOURCES, BuildDirConst.RAW_FILE, BuildDirConst.ARK_DATA, BuildDirConst.SCHEMA);
  }

  getModuleSrcMainResourceRawfileUtdPath(): string {
    return path.resolve(this.getModuleSrcMainPath(), BuildDirConst.RESOURCES, BuildDirConst.RAW_FILE, BuildDirConst.ARK_DATA, BuildDirConst.UTD);
  }

  getModuleSrcMainResourceBaseProfilePath(): string {
    return path.resolve(this.getModuleSrcMainPath(), BuildDirConst.RESOURCES_BASE_PROFILE_DIR);
  }

  getModulePreviewPath(): string {
    return path.resolve(this._modulePath, BuildDirConst.PREVIEW_BUILD_PATH);
  }

  getModuleProductPath(supportBuildCacheDir = true): string {
    return path.resolve(this.getModuleBuildPath(supportBuildCacheDir), this._productName);
  }

  getModuleTestPath(): string {
    return path.resolve(this._modulePath, BuildDirConst.TEST_BUILD_PATH);
  }

  getModulePreviewProductPath(): string {
    return path.resolve(this.getModulePreviewPath(), this._productName);
  }

  /**
   * .preview/${productName}/intermediates/res/${targetName}
   */
  getModulePreviewIntermediatesResPath(): string {
    return path.resolve(this.getModulePreviewProductPath(), BuildDirConst.INTERMEDIATES, BuildDirConst.INTERMEDIATES_RES, this._targetName);
  }

  /**
   * .preview/${productName}/intermediates/res/${targetName}/resources/base/profile
   */
  getModulePreviewIntermediatesProfilePath(): string {
    return path.resolve(this.getModulePreviewIntermediatesResPath(), BuildDirConst.RESOURCES_BASE_PROFILE_DIR);
  }

  getModuleBuildIntermediates(): string {
    return path.resolve(this.getModuleProductPath(), BuildDirConst.INTERMEDIATES);
  }

  getModuleBuildGenerated(): string {
    return path.resolve(this.getModuleProductPath(), BuildDirConst.GENERATED);
  }

  getModulePreviewBuildGenerated(): string {
    return path.resolve(this.getModulePreviewProductPath(), BuildDirConst.GENERATED);
  }

  getModuleBuildOutputPath(): string {
    return path.resolve(this.getModuleProductPath(false), BuildDirConst.BUILD_OUTPUTS, this._targetName);
  }

  getModuleBuildOhosTestOutputPath(): string {
    return path.resolve(this.getModuleProductPath(), BuildDirConst.BUILD_OUTPUTS, CommonConst.OHOS_TEST);
  }

  getIntermediatesMergeRes(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_RES, this._targetName);
  }

  getIntermediatesPacJsonDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.PAC_DIR, this._targetName);
  }

  getIntermediatesPacJsonPath(): string {
    return path.resolve(this.getIntermediatesPacJsonDir(), CommonConst.PAC_JSON);
  }

  getPacJsonPath(): string {
    return path.resolve(this.getModulePath(), CommonConst.PAC_JSON);
  }

  getIntermediatesMergeFile(): string {
    return path.resolve(this.getIntermediatesMergeRes(), BuildArtifactConst.MERGE_RES_FILE_INDEX);
  }

  getIntermediatesMergeProfile(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_PROFILE, this._targetName, CommonConst.MODULE_JSON);
  }

  getIntermediatesProcessProfileDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_PROCESS_PROFILE, this._targetName);
  }

  getGenerateSourceR(): string {
    return path.resolve(this.getModuleBuildGenerated(), BuildDirConst.R, this._targetName);
  }

  getGeneratePmDir(): string {
    return path.resolve(this.getModuleBuildGenerated(), BuildDirConst.PM_DIR, this._targetName);
  }

  getGenerateBuildProfilePath(targetName: string): string {
    return path.resolve(this.getGenerateBuildProfileDir(), targetName, BuildArtifactConst.BUILD_PROFILE_FILE);
  }

  getHarGenerateBuildProfilePath(): string {
    return path.resolve(this._modulePath, BuildArtifactConst.BUILD_PROFILE_FILE);
  }

  getGenerateModuleInfoPath(): string {
    return path.resolve(this.getGenerateBuildProfileTarget(), BuildArtifactConst.MODULE_INFO_FILE);
  }

  getGenerateBuildProfileDir(): string {
    return path.resolve(this.getModuleBuildGenerated(), BuildArtifactConst.PROFILE_DIR);
  }

  getGenerateBuildProfileTarget(): string {
    return path.resolve(this.getGenerateBuildProfileDir(), this._targetName);
  }

  getPreviewGenerateSourceR(): string {
    return path.resolve(this.getModulePreviewBuildGenerated(), BuildDirConst.R, this._targetName);
  }

  getIntermediatesProcessProfile(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_PROCESS_PROFILE, this._targetName, CommonConst.MODULE_JSON);
  }

  getIntermediatesArkModuleJsonPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_RES, this._targetName, CommonConst.ARK_MODULE_JSON);
  }

  getIntermediatesProcessLegacyProfile(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_PROCESS_PROFILE, this._targetName, CommonConst.CONFIG_JSON);
  }

  getIntermediatesRes(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_RES, this._targetName);
  }

  /**
   * 编译过程中sdk中处理生成mock-config.json的路径
   * /build/intermediates/res/${targetName}/mock-config.json
   *
   * @returns {string}
   */
  getIntermediatesMockConfigJsonFile(): string {
    return path.resolve(this.getIntermediatesRes(), BuildArtifactConst.MOCK_CONFIG_JSON);
  }

  /**
   * 中间临时mock目录,打包到目标产物中的mock相关的内容
   * /build/intermediates/res/${targetName}/resources/rawfile/mock
   *
   * @returns {string}
   */
  getIntermediatesResourcesRawFileMockDir() {
    return path.resolve(this.getIntermediatesRes(), BuildDirConst.RESOURCES, BuildDirConst.RAW_FILE, BuildDirConst.MOCK);
  }

  /**
   * restool编译结果的产物profile的路径
   * 其中resources,base,profile是固定的，是restool中的定义
   *
   * @return ../intermediates/res/{targetName}/res/base/profile
   */
  getIntermediatesResProfilePath(): string {
    return path.resolve(this.getIntermediatesRes(), BuildDirConst.RESTOOL_BUILD_RESOURCES, BuildDirConst.BASE, BuildDirConst.PROFILE);
  }

  /**
   * arkData编译结果的产物arkdata路径
   *
   * @return ../intermediates/res/{targetName}/resources/rawfile/arkdata/schema
   */
  getIntermediatesResArkDataPath(): string {
    return path.resolve(
      this.getIntermediatesRes(),
      BuildDirConst.RESTOOL_BUILD_RESOURCES,
      BuildDirConst.RAW_FILE,
      BuildDirConst.ARK_DATA,
      BuildDirConst.SCHEMA,
    );
  }

  getIntermediatesFaAssetsPath(product: ProjectBuildProfile.ProductBuildOpt): string {
    return path.resolve(this.getModuleBuildIntermediates(), `${BuildDirConst.JS_BUILD_ASSETS}_${this._curModule.getCompileMode(product)}`, this._targetName);
  }

  getInterMediatesLoaderOutPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.LOADER_OUT, this._targetName);
  }

  getInterMediatesSourceMapDirPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.SOURCEMAP_INTERMEDIATE_DIR, this._targetName);
  }

  /**
   * 返回intermediate中的sourceMap的路径，若不存在就返回空字符串
   */
  getSourceMapIntermediatePath(): string {
    const intermediateSourceMapPath = path.resolve(this.getInterMediatesSourceMapDirPath(), BuildArtifactConst.SOURCEMAPS_MAP);
    if (fse.existsSync(intermediateSourceMapPath)) {
      return intermediateSourceMapPath;
    }
    return '';
  }

  /**
   * 返回origin的sourceMap的路径,release和debug模式下路径不同
   * har的产物目录跟hap hsp的产物目录下的名称有所不同，需要进行区分
   * @input TargetTaskService
   */
  getSourceMapOriginPath(targetService: TargetTaskService): string {
    const CompileArkFolderName: string = targetService.getModuleService().getModuleModel().isHarModule() ? 'HarCompileArkTS' : 'CompileArkTS';
    return targetService.getBuildOption()?.debuggable
      ? path.resolve(this.getInterMediatesLoaderOutPath(), CodeType.ETS, BuildArtifactConst.SOURCEMAPS_MAP)
      : path.resolve(this.getModuleBuildCachePath(), `${this._targetName}@${CompileArkFolderName}`, 'esmodule', 'release', BuildArtifactConst.SOURCEMAPS_MAP);
  }

  getInterMediatesAssetsPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.JS_BUILD_ASSETS, this._targetName);
  }

  getInterMediatesLoaderOutLitePath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.LOADER_OUT_LITE, this._targetName);
  }

  getIntermediatesLoaderPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.LOADER, this._targetName);
  }

  // 获取hsp/字节码har存放接口类的中间路径.d.ets接口文件的拷贝入口目录，该目录现在使用模块名拼接
  getIntermediatesEtsTgzPath(moduleName: string): string {
    return path.resolve(this.getInterMediatesLoaderOutPath(), BuildDirConst.DECLARE_FILE_OUTPUT, moduleName);
  }

  /**
   * 语义信息表路径
   */
  getIntermediatesPkgContextInfoPath(): string {
    return path.resolve(this.getIntermediatesLoaderPath(), CommonConst.PKG_CONTEXT_INFO_JSON);
  }

  getIntermediatesApPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'ap');
  }

  getModuleBuildCachePath(): string {
    return path.resolve(this.getModuleProductPath(), BuildDirConst.BUILD_CACHE, this._targetName);
  }

  getIntermediatesMergeLegacyProfile(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_MERGE_PROFILE, this._targetName, CommonConst.CONFIG_JSON);
  }

  getIntermediatesLegacyManifestJson(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.LEGACY_MANIFEST, this._targetName);
  }

  getIntermediatesPackInfoDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.LEGACY_MANIFEST, this._targetName);
  }

  getIntermediatesProcessLibs(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.LIBS, this._targetName);
  }

  getIntermediatesStrippedLibsDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.STRIPPED_NATIVE_LIBS, this._targetName);
  }

  getIntermediatesPatch(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.PATCH, this._targetName);
  }

  getIntermediatesSysCap(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.SYSCAP, this._targetName);
  }

  getIntermediatesRouterMap(routerMapFileName: string): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.INTERMEDIATES_ROUTER_MAP_TEMP_DIR, this._targetName, `${routerMapFileName}.json`);
  }

  getIntermediatesStartupPath(startupFileName: string | undefined): string {
    return path.resolve(
      this.getModuleBuildIntermediates(),
      BuildDirConst.INTERMEDIATES_STARTUP_TEMP_DIR,
      this._targetName,
      `${startupFileName ?? BuildDirConst.DEFAULT_STARTUP_CONFIG_FILE_NAME}.json`,
    );
  }

  getCppOutputDir(): string {
    return path.resolve(
      isCI()
        ? InjectUtil.getBuildCacheParentDir(
            this._modulePath,

            // Safety: hvigor.getProject()必然已经被初始化
            path.join(hvigorCore.getProject()!.getName(), this._modulePath.substring(hvigorCore.getProject()!.getNodeDir().length)),
          )
        : this._modulePath,
      BuildDirConst.CPP_OUTPUT,
    );
  }

  getNinjaWorkDir(): string {
    return path.resolve(this.getCppOutputDir(), this._productName, this._targetName, getBuildModeName());
  }

  getModuleShellDir(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.SHELL, this._targetName);
  }

  getShellResourceDir(): string {
    return path.resolve(this.getShellSrcMain(), BuildDirConst.INTERMEDIATES_RES);
  }

  getShellSrcMain(): string {
    return path.resolve(this.getModuleShellDir(), BuildDirConst.SRC, BuildDirConst.MAIN);
  }

  getShellBuildResourceDir(): string {
    return path.resolve(this.getModuleShellDir(), this.getBuildRoot(), BuildDirConst.INTERMEDIATES_RES);
  }

  getIntermediatesCppOutPut(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.CMAKE, this._targetName, BuildDirConst.OBJ);
  }

  getIntermediatesLiteBinSource(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'lite_res', this._targetName);
  }

  getModuleBinOutput(): string {
    return path.resolve(this.getModuleBuildOutputPath(), 'bin');
  }

  getIntermediatesLiteSource(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'lite_source', this._targetName);
  }

  getIntermediatesTemp(): string {
    return path.resolve(this.getModuleBuildIntermediates(), this._targetName, BuildDirConst.TEMP_DIR);
  }

  /**
   * 获取预览和调试对应的编译参数配置文件路径
   *
   * @param isPreview 是否是预览模式
   * @returns {string} 编译参数配置文件路径
   */
  getBuildConfigPath(isPreview?: boolean): string {
    return path.resolve(isPreview ? this.getModulePreviewPath() : this.getModuleBuildPath(), 'config', 'buildConfig.json');
  }

  getIntermediatesOutputMetadata(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.HAP_METADATA, this._targetName, 'output_metadata.json');
  }

  getTestBuildConfigPath(): string {
    return path.resolve(this.getModuleTestPath(), 'config', 'buildConfig.json');
  }

  getIntermediatesHspOutputMetadata(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.HSP_METADATA, this._targetName, BuildArtifactConst.OUTPUT_METADATA_JSON);
  }

  getFromConfigPathByTargetName(moduleModel: CoreModuleModelImpl, targetName: string, formConfigName: string | undefined) {
    if (!formConfigName) {
      return undefined;
    }
    return path.resolve(moduleModel.getSourceSetByTargetName(this._targetName).getTargetResPath(), 'base', 'profile', `${formConfigName}.json`);
  }

  getSyscapJsonPathInfo(moduleModel: ModuleModel): string {
    const sourceRoot = moduleModel.getSourceRootByTargetName(this._targetName);
    return path.resolve(sourceRoot, CommonConst.SYSCAP_JSON);
  }

  getPreviewIntermediatesResModuleJsonPath(): string {
    return path.resolve(this.getModulePreviewIntermediatesResPath(), CommonConst.MODULE_JSON);
  }

  getPreviewIntermediatesMainPagesJsonPath(): string {
    return path.resolve(this.getModulePreviewIntermediatesProfilePath(), BuildArtifactConst.MAIN_PAGES_JSON);
  }

  getUnitTestTemplatePath(): string {
    return path.resolve(this.getModuleBuildPath(), 'testability');
  }

  /**
   * 仓颉构建产物路径
   * @returns {string} 产物路径/intermediates/cj/extend_libs/{targetName}
   */
  getIntermediatesCangjieOutPut(): string {
    return path.resolve(this.getModuleBuildIntermediates(), 'cj', 'extend_libs', this._targetName);
  }

  /**
   * 获取集成态hsp存储目录
   */
  getIntegratedHspOutputPath(): string {
    return path.resolve(this.getModuleBuildOutputPath(), BuildDirConst.INTEGRATED_HSP);
  }

  getDefaultSourceMapPath(): string {
    return path.resolve(this.getModuleBuildPath(), this._productName, BuildDirConst.BUILD_OUTPUTS, this._targetName, BuildDirConst.MAPPING);
  }

  /**
   * 打包工具生成的缓存文件/中间产物路径
   */
  getPackageHapProductPath(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.PACKAGE_HAP, this._targetName);
  }

  /**
   * Intermediate fastapp output path
   */
  getFastAppStorageDirectory(): string {
    return path.resolve(this.getModuleBuildIntermediates(), BuildDirConst.FAST_APP_DIR, this._targetName);
  }

  /**
   * 根据HSP、HAR模块路径获取对顶的 resource_str目录
   */
  getDependencyResourceStr(modulePath: string): string {
    if (InjectUtil.isLocalTest()) {
      return path.resolve(
        modulePath,
        UnitTestConst.TEST_BUILD_ROOT,
        this._productName,
        BuildDirConst.INTERMEDIATES,
        BuildDirConst.INTERMEDIATES_RES,
        this._targetName,
        BuildDirConst.RESOURCE_STR,
      );
    }
    return path.resolve(
      modulePath,
      BuildDirConst.BUILD_ROOT,
      this._productName,
      BuildDirConst.INTERMEDIATES,
      BuildDirConst.INTERMEDIATES_RES,
      this._targetName,
      BuildDirConst.RESOURCE_STR,
    );
  }

  /**
   * 根据app,hap, hsp,字节码har, 源码har 获取对应的symbol路径
   */
  getSymbolsDir(modulePath: string): string {
    return modulePath
      ? path.resolve(modulePath, BuildDirConst.BUILD_ROOT, this._productName, BuildDirConst.BUILD_OUTPUTS, this._targetName, BuildDirConst.OUTPUT_SYMBOL)
      : '';
  }

  /**
   *  获取 resource_str目录
   */
  getResourceStr(): string {
    return path.resolve(this.getIntermediatesRes(), BuildDirConst.RESOURCE_STR);
  }

  /**
   * loader.json路径
   */
  getLoaderJsonPath(): string {
    return path.resolve(this.getIntermediatesLoaderPath(), BuildArtifactConst.LOADER_JSON);
  }

  /**
   * widget_loader.json路径，用于辅助卡片编译
   */
  getWidgetLoaderJsonPath(): string {
    return path.resolve(this.getIntermediatesLoaderPath(), BuildArtifactConst.WIDGET_LOADER_JSON);
  }

  getObfCachePath(): string {
    return path.resolve(
      this.getModuleBuildCachePath(),
      `${this._targetName}@${this._curModule.isHarModule() ? 'HarCompileArkTS' : 'CompileArkTS'}`,
      'esmodule',
      'release',
      'obfuscation'
    );
  }

  getObfNameCachePath(): string {
    return path.resolve(this.getObfCachePath(), BuildArtifactConst.NAME_CACHE);
  }

  getByteCodeObfConfigPath(): string {
    return path.resolve(this.getObfCachePath(), BuildArtifactConst.BYTE_CODE_OBF_CONFIG);
  }

  /**
   * declgenTS 胶水代码生成路径
   */
  getDeclgenBridgeCodePath(): string {
    return path.resolve(
      this.getModuleBuildIntermediates(),
      BuildDirConst.DECLGEN,
      this._targetName,
      BuildDirConst.DECLGEN_BRIDGE_CODE
    );
  }

  /**
   * ArkTS1.2生成的ArkTS1.1的声明文件
   */
  getDeclgenV1OutPath(): string {
    return path.resolve(
      this.getModuleBuildIntermediates(),
      BuildDirConst.DECLGEN,
      this._targetName,
      BuildDirConst.DECLGEN_V1);
  }

  /**
   * ArkTS1.1生成的ArkTS1.2的声明文件
   */
  getDeclgenV2OutPath(): string {
    return path.resolve(
      this.getModuleBuildIntermediates(),
      BuildDirConst.DECLGEN,
      this._targetName,
      BuildDirConst.DECLGEN_V2);
  }

  getDeclgenCachePath(): string {
    return path.resolve(
      this.getModuleBuildIntermediates(),
      BuildDirConst.DECLGEN,
      this._targetName,
      BuildDirConst.BUILD_CACHE);
  }

  getRemoteDependencyCachePath(projectPath: string, packageName: string): string {
    return path.join(
      projectPath,
      BuildDirConst.BUILD_ROOT,
      BuildDirConst.DECLGEN,
      packageName,
      BuildDirConst.BUILD_CACHE
    );
  }

  getRemoteDependencyDeclFileInfoJsonPath(projectPath: string, packageName: string): string {
    return path.join(
      projectPath,
      BuildDirConst.BUILD_ROOT,
      BuildDirConst.DECLGEN,
      packageName,
      BuildDirConst.DECL_FILE_INFO_JSON
    );
  }

  getRemoteDependencyDeclgenV1OutPath(projectPath: string, packageName: string): string {
    return path.join(
      projectPath,
      BuildDirConst.BUILD_ROOT,
      BuildDirConst.DECLGEN,
      packageName,
      BuildDirConst.DECLGEN_V1
    );
  }

  getRemoteDependencyDeclgenV2OutPath(projectPath: string, packageName: string): string {
    return path.join(
      projectPath,
      BuildDirConst.BUILD_ROOT,
      BuildDirConst.DECLGEN,
      packageName,
      BuildDirConst.DECLGEN_V2
    );
  }

  getRemoteDependencyDeclgenBridgeCodePath(projectPath: string, packageName: string): string {
    return path.join(
      projectPath,
      BuildDirConst.BUILD_ROOT,
      BuildDirConst.DECLGEN,
      packageName,
      BuildDirConst.DECLGEN_BRIDGE_CODE
    );
  }

  getDeclFileInfoJsonPath(): string {
    return path.join(
      this.getModuleBuildIntermediates(),
      BuildDirConst.DECLGEN,
      this._targetName,
      BuildDirConst.DECL_FILE_INFO_JSON
    );
  }

  getDeclgenLoaderPath(): string {
    return path.resolve(
      this.getModuleBuildIntermediates(),
      BuildDirConst.DECLGEN,
      this._targetName,
      BuildDirConst.LOADER,
      BuildArtifactConst.LOADER_JSON
    );
  }

  getIntermediatesResInsightIntentJsonPath(): string {
    return path.resolve(this.getIntermediatesResProfilePath(), BuildArtifactConst.INSIGHT_INTENT_JSON);
  }

  /**
   * 仅供ArkTS1.2使用
   * 获取hsp/字节码har存放声明文件的路径，包名拼接
   */
  getIntermediatesDeclgenPath(packageName: string) {
    return path.resolve(this.getDeclgenV2OutPath(), packageName);
  }
}
